﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;


public partial class ybqy_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        SqlConnection sqlcon = new SqlConnection();
        sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
        sqlcon.Open();

        SqlCommand sqlcmd = new SqlCommand("select * from a01000 where  qiyeid=" + Session["qiyeid"], sqlcon);
        string tempp = "select * from a01000 where sbnd='" + DateTime.Now.Year.ToString() + "' and qiyeid=" + Session["qiyeid"];
        SqlDataReader reader = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);

        if (!IsPostBack)
        {
            if (reader.Read())
            {   //先判断是否有今年的
                tempp = "select * from a01000 where sbnd='" + DateTime.Now.Year.ToString() + "' and qiyeid=" + Session["qiyeid"];
                reader.Close();
                sqlcon.Open();
                sqlcmd.CommandText = tempp;
                reader = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);
                if (reader.Read())
                {
                    TextBox1.Text = reader["sbdjdm"].ToString();
                    TextBox4.Text = reader["dwmc"].ToString();
                    TextBox5.Text = reader["fddbr"].ToString();
                    TextBox6.Text = reader["tbr"].ToString();
                    TextBox7.Text = reader["sbdwfrdm"].ToString();
                    if (!CheckZuZhi(TextBox7.Text))
                    {
                        TextBox7.Text = "";
                    }
                    TextBox8.Text = reader["bcrqnian"].ToString();
                    TextBox9.Text = reader["bcrqyue"].ToString();
                    TextBox10.Text = reader["bcrqri"].ToString();
                }
                else
                {
                    tempp = "select max(id) as id from a01000 where  qiyeid=" + Session["qiyeid"];
                    reader.Close();
                    sqlcon.Open();
                    sqlcmd.CommandText = tempp;
                    reader = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);

                    if (reader.Read())
                    {
                        tempp = "select * from a01000 where id=" + reader["id"].ToString() + " and qiyeid=" + Session["qiyeid"];
                        reader.Close();
                        sqlcon.Open();
                        sqlcmd.CommandText = tempp;
                        reader = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);

                        if (reader.Read())
                        {
                            TextBox1.Text = reader["sbdjdm"].ToString();
                            TextBox4.Text = reader["dwmc"].ToString();
                            TextBox5.Text = reader["fddbr"].ToString();
                            TextBox6.Text = reader["tbr"].ToString();
                            TextBox7.Text = reader["sbdwfrdm"].ToString();
                            if (!CheckZuZhi(TextBox7.Text))
                            {
                                TextBox7.Text = "";
                            }
                            TextBox8.Text = reader["bcrqnian"].ToString();
                            TextBox9.Text = reader["bcrqyue"].ToString();
                            TextBox10.Text = reader["bcrqri"].ToString();
                        }
                    }
                }



            }
        }
        reader.Close();

        SqlCommand sqlcmd1 = new SqlCommand("select qhdm from system_status", sqlcon);
        sqlcon.Open();
        TextBox3.Text = sqlcmd1.ExecuteScalar().ToString();
        TextBox2.Text = DateTime.Now.Year.ToString();
        Session["ShenBaoNianDu"] = TextBox2.Text;

        reader.Close();
        sqlcon.Close();

    }

    public string DisplayStr()
    {
        string biaoid = "101000";
        SqlConnection sqlcon = new SqlConnection();
        sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
        sqlcon.Open();

        SqlCommand sqlcmd1 = new SqlCommand("select * from tishi where substring(tishiid,1,6)='" + biaoid + "'", sqlcon);
        //reader.Close();
        //sqlcon.Open();
        SqlDataReader reader1 = sqlcmd1.ExecuteReader(CommandBehavior.CloseConnection);
        string joinstr = "[";
        while (reader1.Read())
        {
            joinstr = joinstr + "{'tishiid':'" + reader1["tishiid"].ToString() + "','idmingcheng':'" + reader1["idmingcheng"].ToString() + "','ti1':'" + reader1["ti1"].ToString() + "','ti2':'" + reader1["ti2"].ToString() + "','ti3':'" + reader1["ti3"].ToString() + "'},";
        }

        joinstr = joinstr.Substring(0, joinstr.Length - 1);
        joinstr = joinstr + "]";

        //TextBox99.Text = joinstr;
        reader1.Close();
        sqlcon.Close();

        return joinstr;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label3.Text = "更新中...";
        TextBox100.Text = "";
        SqlConnection sqlcon = new SqlConnection();
        sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
        sqlcon.Open();

        SqlCommand sqlcmd = new SqlCommand("select * from  a01000 where sbnd='" + DateTime.Now.Year.ToString() + "' and qiyeid=" + Session["qiyeid"], sqlcon);

        SqlDataReader reader = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);
        //Response.Write(TextBox3.Text);

        if (reader.Read())
        {
            string sqlstr = "update a01000 set sbdjdm='" + this.TextBox1.Text + "',sbnd='" + TextBox2.Text;
            sqlstr = sqlstr + "',xzqhdm='" + (TextBox3.Text) + "',dwmc='" + this.TextBox4.Text + "',fddbr='" + TextBox5.Text;
            sqlstr = sqlstr + "',tbr='" + TextBox6.Text + "',sbdwfrdm='" + TextBox7.Text + "',bcrqnian='" + TextBox8.Text + "',bcrqyue='" + TextBox9.Text + "',bcrqri='" + TextBox10.Text + "',bcrq='" + TextBox8.Text + "-" + TextBox9.Text + "-" + TextBox10.Text + "' where sbnd='" + Session["ShenBaoNianDu"] + "' and  qiyeid=" + Session["qiyeid"];

            reader.Close();
            sqlcon.Open();
            SqlCommand sqlcmd1 = new SqlCommand(sqlstr, sqlcon);

            sqlcmd1.ExecuteNonQuery();

        }
        else
        {
            string sqlstr = @"insert into  a01000 ([qiyeid],[sbnd] ) values (" + Session["qiyeid"] + ",'" + Session["ShenBaoNianDu"] + "')";

            //Response.Write(sqlstr);

            reader.Close();
            sqlcon.Open();
            SqlCommand sqlcmd1 = new SqlCommand(sqlstr, sqlcon);

            sqlcmd1.ExecuteNonQuery();

            sqlstr = "update a01000 set sbdjdm='" + this.TextBox1.Text + "',sbnd='" + TextBox2.Text;
            sqlstr = sqlstr + "',xzqhdm='" + (TextBox3.Text) + "',dwmc='" + this.TextBox4.Text + "',fddbr='" + TextBox5.Text;
            sqlstr = sqlstr + "',tbr='" + TextBox6.Text + "',sbdwfrdm='" + TextBox7.Text + "',bcrqnian='" + TextBox8.Text + "',bcrqyue='" + TextBox9.Text + "',bcrqri='" + TextBox10.Text + "',bcrq='" + TextBox8.Text + "-" + TextBox9.Text + "-" + TextBox10.Text + "' where sbnd='" + Session["ShenBaoNianDu"] + "' and  qiyeid=" + Session["qiyeid"];

            reader.Close();
            sqlcon.Close();
            sqlcon.Open();
            SqlCommand sqlcmd2 = new SqlCommand(sqlstr, sqlcon);

            sqlcmd2.ExecuteNonQuery();

        }

        reader.Close();
        sqlcon.Close();


        string jy = "";
        int yzcw = 0;

        if (TextBox4.Text == "")
        {
            TextBox4.BackColor = System.Drawing.Color.Red;
            jy = "单位名称不能为空。\n";
            yzcw = 1;
        }

        if (TextBox7.Text == "")
        {
            TextBox7.BackColor = System.Drawing.Color.Red;
            jy = jy + "申报单位法人代码不能为空。\n";
            yzcw = 1;
        }
        if (!CheckZuZhi(TextBox7.Text))
        {
            TextBox7.BackColor = System.Drawing.Color.Yellow;
            jy = jy + "申报单位法人代码不正确。\n";
            yzcw = 1;
        }


        if (yzcw == 0)
        {
            Label3.Text = "更新完成";
        }
        else
        {
            Label3.Text = "更新有错误";
            TextBox100.Text = jy;
        }
    }

    public static bool CheckZuZhi(string SVIN)
    {
        Boolean Bool_R = false;
        string R_VIN = "";
        string VIN = SVIN.ToUpper().Trim();
        //string sVR = "";
        string s = "";
        string ShuZhi = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //	'字母的数值

        int[] JiaQuan = new int[17]; //{ 0, 3,7,9,10,5,8,4,2 }
        JiaQuan[1] = 3;
        JiaQuan[2] = 7;
        JiaQuan[3] = 9;
        JiaQuan[4] = 10;
        JiaQuan[5] = 5;
        JiaQuan[6] = 8;
        JiaQuan[7] = 4;
        JiaQuan[8] = 2;
        Bool_R = false;//VIN = VIN.ToUpper().Trim();
        if (VIN.Length < 9)
        {
            
            R_VIN = "输入了" + VIN.Length.ToString() + "位字符，不能构成10位组织机构代码证书";
        }
        else
        {
            VIN = VIN.Substring(0, 8) + "-" + VIN.Substring(VIN.Length - 1, 1);
            int i1 = 0;
            string s1 = "";
            for (int i = 1; i <= 10; i++)
            {
                s = VIN.Substring(i - 1, 1);
                int i2 = 0;
                if ((ShuZhi.IndexOf(s) == -1) && (i != 9))
                {
                    s1 = s1 + "^";
                }
                else
                {
                    s1 = s1 + "";
                    i2 = ShuZhi.IndexOf(s) * JiaQuan[i];
                    i1 = i1 + i2;
                }
            }
            if (s1 != "")
            {
                R_VIN = s1 + "所输入的字符中含有非法字符，不能构成10位组织机构代码证书";
            }
            else
            {
                int i2 = i1 % 11;
                i2 = 11 - i2;
                s = i2.ToString();
                if (i2 == 10)
                {
                    s = "X";
                }
                if (s == VIN.Substring(9, 1))
                {
                    R_VIN = VIN + "组织机构代码证书号码校验正确";
                    Bool_R = true;
                }
                else
                {
                    R_VIN = VIN + "  " + s + "组织机构代码证书号码校验不正确!!!!!!";
                    Bool_R = false;
                }
            }
        }
        return Bool_R;
        //return R_VIN;
    }
    protected void TextBox5_TextChanged(object sender, EventArgs e)
    {

    }
    protected void TextBox3_TextChanged(object sender, EventArgs e)
    {

    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
}
